"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A3检测项确保客户端认证超时时间配置检测与修复成功
Create At   : 2024/10/25
Owner       : lihongji
Description :
    1.设置authentication_timeout参数为600后检测A3项
    2.修复A3不符合安全规范项目后再次检测
    3.设置authentication_timeout参数为1后检测A3项
    4.修复A3不符合安全规范项目后再次检测
    5.设置authentication_timeout参数为10min后检测A3项
    6.修复A3不符合安全规范项目后再次检测
    7.登入数据库查看authentication_timeout参数值
    8.设置authentication_timeout参数为1min后检测A3项
    9.设置authentication_timeout参数为60后检测A3项
    10.修复A3符合安全规范项目后再次检测
Expect      :
    1.设置成功，打印authentication_timeout告警信息
    2.不打印告警信息
    3.设置成功，打印authentication_timeout告警信息
    4.不打印告警信息
    5.设置成功，打印authentication_timeout告警信息
    6.不打印告警信息
    7.参数authentication_timeout的值为1min
    8.设置成功，不打印authentication_timeout告警信息
    9.设置成功，不打印authentication_timeout告警信息
    10.不打印告警信息，参数authentication_timeout的值为1min
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):

        step = ('----step1:设置authentication_timeout参数为600后检测A3项 '
                'expect:设置成功，打印authentication_timeout告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout=600")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct client authentication timeout "
                      "configuration.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)

        step = '----step2:修复A3不符合安全规范项目后再次检测 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B3 --detail;'
                          f'gs_checkse -i A3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)

        step = ('----step3:设置authentication_timeout参数为1后检测A3项 '
                'expect:设置成功，打印authentication_timeout告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout=1")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct client authentication timeout "
                      "configuration.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)

        step = '----step4:修复A3不符合安全规范项目后再次检测 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B3 --detail;'
                          f'gs_checkse -i A3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)

        step = ('----step5:设置authentication_timeout参数为10min后检测A3项'
                ' expect:设置成功，打印authentication_timeout告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout='10min'")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct client authentication timeout "
                      "configuration.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)

        step = '----step6:修复A3不符合安全规范项目后再次检测 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B3 --detail;'
                          f'gs_checkse -i A3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)

        step = ('----step7:登入数据库查看authentication_timeout参数值 '
                'expect:参数authentication_timeout的值为1min----')
        self.log.info(step)
        check_sql = "show authentication_timeout"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('1min', create_res, '执行失败' + step)

        step = ('----step8:设置authentication_timeout参数为1min后检测A3项'
                ' expect:设置成功，不打印authentication_timeout告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout='1min'")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)

        step = ('----step8:设置authentication_timeout参数为1min后检测A3项'
                ' expect:设置成功，不打印authentication_timeout告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout='1min'")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)

        step = ('----step9:设置authentication_timeout参数为60后检测A3项'
                ' expect:设置成功，不打印authentication_timeout告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout=60")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)

        step = '----step10:修复A3不符合安全规范项目后再次检测 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B3 --detail;'
                          f'gs_checkse -i A3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct client authentication timeout "
                         "configuration.", check_res, '执行失败' + step)
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('1min', create_res, '执行失败' + step)

    def tearDown(self):
        step = '----step11:无需清理环境 expect:成功----'
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "authentication_timeout='1min'")
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')